package ken.spring.cloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import ken.spring.cloud.alibaba.po.User;
import ken.spring.cloud.alibaba.service.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * <ul>
 * <li>Title: TestController</li>
 * <li>Description: TODO </li>
 * </ul>
 *
 * @author swc
 * @date 2022-06-21 19:44
 */
@RestController
public class TestController {

    @Autowired
    private EchoService echoService =null;

    @GetMapping(value = "/hello")
    //注释用于识别资源是速率限制还是降级。在上面的示例中，注解的 'hello' 属性指的是资源名称。
    @SentinelResource(value = "hello",fallback = "fallback1")
    public String hello() {
        return "Hello Sentinel";
    }

    public String fallback1(Throwable t) {
        if (BlockException.isBlockException(t)) {
            return "Blocked by Sentinel: " + t.getClass().getSimpleName();
        }
        return "Oops, failed: " + t.getClass().getCanonicalName();
    }

    @GetMapping(value = "/echo/{string}")
    public String echo(@PathVariable String string) {
        return echoService.echo(string);
    }

    @GetMapping("/user/list")
    public List<User> queryAll() {
        return echoService.queryAll();
    }

}
